package com.iscale.util;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class ZxingUtil {

    /**
     * @throws WriterException
     * @throws IOException
     * @Author ouyangan
     * @Date
     * @Description 二维码
     */
    public static BufferedImage genaratorQRcode(String url, String code) throws WriterException, IOException {
        int width = 200;
        int height = 200;
        Map<EncodeHintType, Object> hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        hints.put(EncodeHintType.ERROR_CORRECTION, "H");
        hints.put(EncodeHintType.MARGIN, "1");
        BitMatrix bitMatrix = new MultiFormatWriter().encode(url + "?code=" + code,
                BarcodeFormat.QR_CODE, width, height, hints);// 生成矩阵
        BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(bitMatrix);
        return bufferedImage;
    }

    /**
     * @throws WriterException
     * @throws IOException
     * @Author ouyangan
     * @Date
     * @Description 条形码
     */
    public static BufferedImage genaratorBarCode(String code) throws IOException, WriterException {
        int width = 200;
        int height = 70;
        Map<EncodeHintType, Object> hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        hints.put(EncodeHintType.ERROR_CORRECTION, "H");
        hints.put(EncodeHintType.MARGIN, "1");
        BitMatrix bitMatrix = new MultiFormatWriter().encode(code,
                BarcodeFormat.CODE_128, width, height, hints);// 生成矩阵
        BufferedImage bufferedImage = MatrixToImageWriter.toBufferedImage(bitMatrix);
        return bufferedImage;
    }

    /**
     * @param code   编码
     * @param top    二维码
     * @param bottom 条形码
     *
     * @throws IOException
     * @Author ouyangan
     * @Date
     * @Description 合成二维码, 条形码
     */
    public static InputStream genaratorCombineImageToInputStream(String code, BufferedImage top, BufferedImage bottom) throws IOException {
        BufferedImage image = new BufferedImage(top.getWidth(), top.getHeight() + bottom.getHeight() + 40, BufferedImage.TYPE_INT_RGB);
        Graphics graphics = image.getGraphics();
        graphics.setColor(Color.white);
        graphics.fillRect(0, 0, top.getWidth(), top.getHeight() + bottom.getHeight() + 100);
        graphics.setColor(Color.black);
        graphics.drawString("编号:" + code, 12, 20);
        graphics.drawImage(top, 0, 25, null);
        graphics.drawImage(bottom, 0, top.getHeight() + 30, null);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageIO.write(image, "jpg", os);
        InputStream is = new ByteArrayInputStream(os.toByteArray());
        return is;
    }

    /**
     * @param url    url
     * @param code   编码
     * @param top    二维码
     * @param bottom 条形码
     *
     * @throws IOException
     * @Author ouyangan
     * @Date
     * @Description 合成二维码, 条形码
     */
    public static File genaratorCombineImageToFile(String code, String filePath, BufferedImage top, BufferedImage bottom) throws IOException {
        BufferedImage image = new BufferedImage(top.getWidth(), top.getHeight() + bottom.getHeight() + 40, BufferedImage.TYPE_INT_RGB);
        Graphics graphics = image.getGraphics();
        graphics.setFont(new Font("宋体", Font.PLAIN, 12));
        graphics.setColor(Color.white);
        graphics.fillRect(0, 0, top.getWidth(), top.getHeight() + bottom.getHeight() + 100);
        graphics.setColor(Color.black);
        String title = "编号:" + code;
        graphics.drawString(title, 12, 20);
        graphics.drawImage(top, 0, 25, null);
        graphics.drawImage(bottom, 0, top.getHeight() + 30, null);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        File file = new File(filePath + File.separator + "batchGenerator" + File.separator + code + ".jpg");
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        file.createNewFile();
        ImageIO.write(image, "jpg", file);
        return file;
    }
}
